#include "KeySchedule.h"
#include "common.h"
#include "basicoperation.h"


void KeySchedule( byte *user_key , word *S )
{
	word L[c] ;
	for ( int row = 0 ; row < c ; ++row )
	{
		L[row] = (user_key[4*row+3]<<24) ^ (user_key[4*row+2]<<16) ^ (user_key[4*row+1]<<8) ^ user_key[4*row] ;
	}
	S[0] = P32 ;
	for ( int count = 1 ; count <= 2*r+3 ; ++count )
	{
		S[count] = S[count-1] + Q32 ;
	}
	word A , B , i , j ,v ;
	A=B=i=j=0 ;
	v = 3*max( c , 2*r+4 ) ;
	for ( int s = 1 ; s<=v ; ++s )
	{
		A = S[i] =rotate_left( S[i]+A+B , last_5_bits(3) ) ;
		B = L[j] = rotate_left( L[j]+A+B , last_5_bits(A+B) ) ;
		i = (i+1) % (2*r+4) ;
		j = (j+1) % c ;
	}
}